home *** CD-ROM | disk | FTP | other *** search
/ Compendium Deluxe 1 / LSD Compendium Deluxe 1.iso / a / programming / assemblers / cas.lha / data / data.s < prev    next >
Encoding:
Text File  |  1992-08-10  |  12.3 KB  |  474 lines

  1. include "8051fa.h"
  2. include "kernel.h"    ;;; This MUST be the first module included.
  3. include "math.h"
  4. include "stdio.h"
  5.  
  6. ;;; Timing
  7. seg data
  8. global CTIME: ds 2
  9. global TIME:  ds 2
  10.  
  11. seg code
  12. SetCEX:
  13.    mov CMOD, #ECF
  14.    mov CCAPM0, #(CAPN + ECCF)
  15.    mov CCAPM1, #(CAPN + ECCF)
  16.    mov CCAPM2, #(CAPN + ECCF)
  17.    mov CCAPM3, #(CAPN + ECCF)
  18.    mov CCAPM4, #(CAPN + ECCF)   ;;; Set CEX inputs for falling edge mode.
  19.    setb CEX0
  20.    setb CEX1
  21.    setb CEX2
  22.    setb CEX3
  23.    setb CEX4                    ;;; Latch CEX inputs for input.
  24.    clr PPC                      ;;; Set interrupt for low priority.
  25. ret
  26.  
  27. SetT2EX:
  28.    setb T2EX
  29.    setb CP_RL2
  30.    setb EXEN2  ;;; Enable captures in T2EX.
  31.    clr C_T2    ;;; Set TIMER2 as a timer.
  32.    clr PT2     ;;; And set its interrupt priority low.
  33. ret
  34.  
  35. SetTF0:
  36.    mov A, TMOD
  37.    anl A, #11110000b
  38.    add A, #01h      ;;; Timer 0: 16-bit internal timer.
  39.    mov TMOD, A
  40.    clr PT0          ;;; Set for low priority.
  41. ret
  42.  
  43. SetCClock:
  44.    mov CL, #0
  45.    mov CH, #0
  46.    mov CTIME, #0
  47.    mov (CTIME + 1), #0 ;;; CTIME = 0;
  48.    clr CF
  49.    setb EC
  50.    setb CR             ;;; CLOCK ON;
  51. 1:                     ;;; while (1) {
  52. ret                    ;;;    "pause on CF";
  53. global CTick:
  54.    inc CTIME
  55.    mov A, CTIME
  56.    jnz 2f
  57.       inc (CTIME + 1)
  58.    2:                  ;;;    CTIME++;
  59. sjmp 1b                ;;; }
  60.  
  61. SetClock:
  62.    mov TL2, #0
  63.    mov TH2, #0
  64.    mov TIME, #0
  65.    mov (TIME + 1), #0 ;;; TIME = 0;
  66.    clr TF2
  67.    setb ET2
  68.    setb TR2           ;;; CLOCK ON;
  69. 1:                    ;;; while (1) {
  70. ret
  71. global Tick:          ;;;    "pause on TF2";
  72.    inc TIME
  73.    mov A, TIME
  74.    jnz 2f
  75.       inc (TIME + 1)
  76.    2:                 ;;;    TIME++;
  77. sjmp 1b               ;;; }
  78.  
  79. Capture: ;;; "capture the time of the last sensor";
  80.    mov (TIME0 + 0), TL2
  81.    mov (TIME0 + 1), TH2
  82.    mov (TIME0 + 2), (TIME + 0)
  83.    mov (TIME0 + 3), (TIME + 1)
  84.    mov (TIME1 + 0), CL
  85.    mov (TIME1 + 1), CH
  86.    mov (TIME1 + 2), (CTIME + 0)
  87.    mov (TIME1 + 3), (CTIME + 1)
  88.    mov (TIME2 + 0), CL
  89.    mov (TIME2 + 1), CH
  90.    mov (TIME2 + 2), (CTIME + 0)
  91.    mov (TIME2 + 3), (CTIME + 1)
  92.    mov (TIME3 + 0), CL
  93.    mov (TIME3 + 1), CH
  94.    mov (TIME3 + 2), (CTIME + 0)
  95.    mov (TIME3 + 3), (CTIME + 1)
  96.    mov (TIME4 + 0), CL
  97.    mov (TIME4 + 1), CH
  98.    mov (TIME4 + 2), (CTIME + 0)
  99.    mov (TIME4 + 3), (CTIME + 1)
  100.    mov (TIME5 + 0), CL
  101.    mov (TIME5 + 1), CH
  102.    mov (TIME5 + 2), (CTIME + 0)
  103.    mov (TIME5 + 3), (CTIME + 1)
  104. ret
  105.  
  106. ARR_BASE: dw 0080h, 15c0h, 2b00h, 3f40h, 5580h, 6ac0h
  107. GetBase:
  108.    mov A, Counter
  109.    rl A
  110.    mov B, A
  111.    mov DPTR, #ARR_BASE
  112.    movc A, @A + DPTR
  113.    xch A, B              ;;; B = ARR_BASE[2*Counter]
  114.    inc A
  115.    movc A, @A + DPTR     ;;; A = ARR_BASE[2*Counter + 1];
  116.    mov Bot, A
  117.    mov (Bot + 1), B      ;;; Bot = ((word *)ARR_BASE)[Counter];
  118. ret
  119. GetSize:
  120.    mov A, Counter
  121.    rl A
  122.    add A, #Cycles
  123.    mov R0, A
  124.    mov A, @R0
  125.    mov B, R2
  126.    mul AB
  127.    mov Size, A
  128.    mov (Size + 1), B
  129.    inc R0
  130.    mov A, @R0
  131.    mov B, R2
  132.    mul AB
  133.    add A, (Size + 1)
  134.    xch A, (Size + 1)  ;;; Size = R2 * ((word *)Cycles)[Counter];
  135. ret
  136.  
  137. Store:
  138.    mov A, Bot
  139.    add A, Size
  140.    mov DPL, A
  141.    mov A, (Bot + 1)
  142.    addc A, (Size + 1)
  143.    mov DPH, A             ;;; for (DPTR = Bot + Size; R2 > 0; R2--) {
  144.    1:
  145.       mov A, @R0
  146.       inc R0
  147.       movx @DPTR, A
  148.       inc DPTR            ;;;    *DPTR++ = R0++;
  149.    djnz R2, 1b            ;;; }
  150. ret
  151.  
  152. ;;; All the statistical variables for the 6 inputs are replicated 6 times.
  153. SECTORS equ 20
  154. seg data
  155. global Sector0: ds 1
  156. global Sector1: ds 1
  157. global Sector2: ds 1
  158. global Sector3: ds 1
  159. global Sector4: ds 1
  160. global Sector5: ds 1
  161.  
  162. seg data
  163. StatSeg:
  164. global TIME0: ds 4
  165. global TIME1: ds 4
  166. global TIME2: ds 4
  167. global TIME3: ds 4
  168. global TIME4: ds 4
  169. global TIME5: ds 4
  170. Cycles:  ds 2*6
  171. First:   ds 4*6
  172. Last:    ds 4*6
  173. Square:  ds 8*6
  174. STAT_SIZE equ $ - StatSeg
  175.  
  176. seg data at 0x30
  177. Width:  ds 4
  178. Bot:    ds 2
  179. Size:   ds 2
  180.  
  181. seg code
  182. SetStats:
  183.    mov R0, #Sector0
  184.    mov R2, #6
  185.    1:
  186.       mov @R0, #1
  187.       inc R0
  188.    djnz R2, 1b           ;;; Set all sector counts to 0;
  189.    mov State, #00111111b ;;; Set all FXn to 1, and clear the abort flag FXX.
  190.    mov R0, #StatSeg
  191.    mov R2, #STAT_SIZE
  192.    acall ClearX          ;;; Clear all statistical registers;
  193. ret
  194. global Partial:          ;;; "pause on PULSE";
  195.    mov A, Counter; rl A; rl A; add A, #First
  196.    mov R0, A
  197.    add A, #(TIME0 - First)
  198.    mov R1, A
  199.    mov R2, #4
  200.    acall CopyX           ;;;    First = TIME;
  201. StatsLoop:               ;;;    while (1) {
  202.    mov A, Counter; rl A; rl A; add A, #Last
  203.    mov R0, A
  204.    add A, #(TIME0 - Last)
  205.    mov R1, A
  206.    mov R2, #4
  207.    acall CopyX           ;;;       Last = TIME;
  208. ret
  209. global Nutation:         ;;;       "pause on PULSE";
  210.    mov R0, #Width
  211.    mov A, Counter; rl A; rl A; add A, #TIME0
  212.    mov R1, A
  213.    mov R2, #4
  214.    acall CopyX           ;;;      Width = TIME;
  215.    mov R0, #Width
  216.    mov A, Counter; rl A; rl A; add A, #Last
  217.    mov R1, A
  218.    mov R2, #4
  219.    acall SubtractX       ;;;      Width -= Last;
  220.    acall GetBase         ;;;      Bot = ARR_BASE;
  221.    mov R2, #4
  222.    acall GetSize         ;;;      Size = 4*Cycles;
  223.    mov R0, #Width
  224.    mov R2, #4
  225.    acall Store           ;;;      Bot[Size] = Width;
  226.    mov A, Counter; rl A; add A, #Cycles
  227.    mov R0, A
  228.    mov R2, #2
  229.    acall IncX            ;;;      Cycles++;
  230.    mov ArgX, #Width
  231.    mov SizeX, #4
  232.    mov ArgY, #Width
  233.    mov SizeY, #4
  234.    mov A, Counter; rl A; rl A; rl A; add A, #Square
  235.    mov ArgZ, A
  236.    acall MultiplyX       ;;;      Square += Width*Width;
  237. sjmp StatsLoop           ;;; }
  238.  
  239. SetIE0:
  240.    setb INT0 ;;; Latch INT0 for input.
  241.    setb IT0
  242.    clr PX0   ;;; And set its interrupt priority low (should be high).
  243. ret
  244.  
  245. ClearFlags:
  246.    clr EXF2
  247.    clr CCF0
  248.    clr CCF1
  249.    clr CCF2
  250.    clr CCF3
  251.    clr CCF4
  252.    mov TSTATUS, #0
  253.    mov CSTATUS, #0
  254. ret
  255.  
  256. SetTest:
  257.    acall SetIE0          ;;; Enable IE0;
  258.    acall SetT2EX         ;;; Enable TF2, EXF2;
  259.    acall SetCEX          ;;; Enable CF, CCFn;
  260.    acall SetTF0          ;;; Enable process scheduler.
  261.    mov State, #01000000b ;;; Set abort flag, clear all others.
  262. ret
  263. TestCycle:
  264.    acall SetStats
  265.    clr IE0
  266.    setb EX0         ;;; "turn on IE0";
  267.    mov R0, #SP_IE0
  268.    acall Pause      ;;; "pause on IE0, first sensor";
  269.    acall SetClock
  270.    acall SetCClock
  271.    acall ClearFlags
  272.    acall Scheduler
  273.    clr IE0
  274.    mov R0, #SP_IE0
  275.    acall Pause      ;;; "pause on IE0, second sensor";
  276.    clr EX0          ;;; "turn off IE0";
  277.    clr TR0
  278.    clr ET0
  279.    clr TR2
  280.    clr ET2          ;;; "turn off TF2, EXF2";
  281.    clr CR
  282.    clr EC           ;;; "turn off CF, CCF0, CCF1, CCF2, CCF3, CCF4";
  283.    acall Capture    ;;; "save the time of the last sensor";
  284. ret
  285.  
  286. ;;; Data transfer
  287. ;;; # SIZE <Data> Sum <-- Packet
  288. SendDBY: ;;; R0: Base, R2: Size
  289.    mov A, #'#'
  290.    acall putchar     ;;; putchar('#');
  291.    mov A, #23
  292.    acall putchar     ;;; putchar(23);
  293.    mov A, State
  294.    acall putchar     ;;; putchar(State);
  295.    mov R3, State
  296.    mov A, Counter
  297.    rl A
  298.    mov R7, A
  299.    add A, #Cycles
  300.    mov Bot, A
  301.    mov R2, #2
  302.    acall DBYLoop     ;;; Send(Cycles[Counter]);
  303.    mov R0, #Width
  304.    mov A, R7
  305.    rl A
  306.    mov R7, A
  307.    add A, #TIME0
  308.    mov Bot, A
  309.    mov R2, #4
  310.    acall DBYLoop     ;;; Send(TIME[Counter]);
  311.    mov A, R7
  312.    add A, #First
  313.    mov Bot, A
  314.    mov R2, #4
  315.    acall DBYLoop     ;;; Send(First[Counter]);
  316.    mov A, R7
  317.    add A, #Last
  318.    mov Bot, A
  319.    mov R2, #4
  320.    acall DBYLoop     ;;; Send(Last[Counter]);
  321.    mov A, R7
  322.    rl A
  323.    add A, #Square
  324.    mov Bot, A
  325.    mov R2, #8
  326.    acall DBYLoop     ;;; Send(Square[Counter]);
  327.    mov A, R3
  328.    acall putchar     ;;; Send(CheckSum);
  329. ret
  330.  
  331. DBYLoop:
  332.    1:
  333.       mov R0, Bot
  334.       inc Bot
  335.       mov A, @R0
  336.       xch A, R3
  337.       add A, R3
  338.       xch A, R3
  339.       acall putchar
  340.    djnz R2, 1b
  341. ret
  342.  
  343. ;;; : SIZE <Data> Sum  <-- Packet SIZE == 0 means a full packet: 100h bytes.
  344. ;;; .                  <-- End of transmission
  345. RETRIES equ 3
  346. SendXBY:
  347.    mov R4, #RETRIES    ;;; Retry = RETRIES;
  348.    mov A, Size
  349.    orl A, (Size + 1)
  350. jnz SendPacket         ;;; if (Size == 0) {
  351.    mov A, #'.'
  352.    acall putchar       ;;;    putchar('.'); return;
  353. ret                    ;;; }
  354. SendPacket:
  355.    mov A, #':'
  356.    acall putchar       ;;; putchar(':');
  357.    mov A, Size
  358.    acall putchar       ;;; putchar(Size);
  359.    mov R3, #0          ;;; CheckSum = 0;
  360.    mov DPL, Bot
  361.    mov DPH, (Bot + 1)
  362.    mov R2, Size
  363.    1:                  ;;; for (DPTR = Bot, R2 = Size; R2 > 0; R2--) {
  364.       movx A, @DPTR
  365.       inc DPTR
  366.       xch A, R3
  367.       add A, R3
  368.       xch A, R3
  369.       acall putchar    ;;;    putchar(A = *DPTR++), CheckSum += A;
  370.    djnz R2, 1b         ;;; }
  371.    mov A, R3
  372.    acall putchar       ;;; putchar(CheckSum);
  373.    acall getchar       ;;; switch (getchar()) {
  374. 1: cjne A, #1bh, 1f    ;;;    case NAK:
  375.    djnz R4, 2f         ;;;       if (--Retry == 0) {
  376.       ret              ;;;          return;
  377.    2:                  ;;;       }
  378. ajmp SendPacket        ;;;    resend the packet;
  379. 1: cjne A, #1ch, 1f    ;;;    case ACK:
  380.    mov A, Size
  381.    jz 2f               ;;;       if (Size%0x100 != 0) {
  382.       mov Size, #0     ;;;          Size -= Size%0x100;
  383.    sjmp 3f             ;;;       }
  384.    2:                  ;;;       else {
  385.       dec (Size + 1)   ;;;          Size -= 0x100;
  386.    3:                  ;;;       }
  387.    mov Bot, DPL
  388.    mov (Bot + 1), DPH  ;;;       Bot = DPTR;
  389. ajmp SendXBY           ;;;    send the next packet;
  390. 1: ret                 ;;;    default: abort;
  391.  
  392. ;;; Command Interpreter:
  393. ;;; Data format (in octal):
  394. ;;; 000 + m -- Stats m
  395. ;;; 010 + m -- Dump m
  396. ;;; 020     -- Status
  397. ;;; 030     -- Test
  398. ;;; 037     -- Abort
  399. Interpreter:              ;;; Interpreter() {
  400.    acall SetPort          ;;;    open the serial port at 9600,N,9,1;
  401. CommandLoop:              ;;;    while (1) {
  402.    acall getchar          ;;;       A = getchar();
  403.    mov Counter, A
  404.    anl A, #00111000b
  405.    xch A, Counter
  406.    anl A, #00000111b
  407.    xch A, Counter         ;;;       Counter = A&7, switch (A&0370) {
  408. 1: cjne A, #00h, 1f       ;;;       case 000:
  409. jb EX0, CommandLoop       ;;;          if (in test) break;
  410.    mov A, Counter
  411.    cjne A, #6, $ + 3
  412. jnc CommandLoop           ;;;          if (Counter >= 6) break;
  413.    acall SendDBY          ;;;          SendDBY(Counter);
  414. sjmp CommandLoop          ;;;       break;
  415. 1: cjne A, #08h, 1f       ;;;       case 010:
  416. jb EX0, CommandLoop       ;;;          if (in test) break;
  417.    mov A, Counter
  418.    cjne A, #6, $ + 3
  419. jnc CommandLoop           ;;;          if (Counter >= 6) break;
  420.    acall GetBase          ;;;          Bot = ARR_BASE[Counter];
  421.    mov R2, #4
  422.    acall GetSize          ;;;          Size = 4*Cycles[Counter];
  423.    acall SendXBY          ;;;          SendXBY(Bot, Size);
  424. sjmp CommandLoop          ;;;       break;
  425. 1: cjne A, #10h, 1f       ;;;       case 020:
  426.    mov A, Counter
  427. cjne A, #0, CommandLoop   ;;;          if (Counter != 0) break;
  428.    jnb ET2, 2f            ;;;          if (in test && counting) {
  429.       mov A, #'2'         ;;;             putchar('2');
  430.    sjmp 3f                ;;;          }
  431.    2:                     ;;;          else
  432.    jnb EX0, 2f            ;;;          if (in test) {
  433.       mov A, #'1'         ;;;             putchar('1`);
  434.    sjmp 3f                ;;;          }
  435.    2:                     ;;;          else {
  436.       mov A, #'0'         ;;;             putchar('0');
  437.    3:                     ;;;          }
  438.    acall putchar
  439. sjmp CommandLoop          ;;;       break;
  440. 1: cjne A, #18h, 1f       ;;;       case 030:
  441.    mov A, Counter         ;;;          switch (Counter) {
  442.    2: cjne A, #00h, 2f       ;;;       case 0:
  443.       jb EX0, CommandLoop    ;;;          if (in test) break;
  444.       mov R0, #TEST_SEG
  445.       mov DPTR, #TestCycle
  446.       acall Spawn            ;;;          Spawn(0xe0, TestCycle);
  447.    sjmp CommandLoop          ;;;       break;
  448.    2: cjne A, #07h, 2f       ;;;       case 7:
  449.       jnb EX0, CommandLoop   ;;;          if (!in test) break;
  450.       clr EX0                ;;;          "turn off sensor";
  451.       clr TR0                ;;;          "turn off the scheduler";
  452.       clr ET0
  453.       clr TR2
  454.       clr ET2                ;;;          "turn off timer 2";
  455.       clr CR
  456.       clr EC                 ;;;          "turn off the capture timer";
  457.       acall Capture          ;;;          "save the time of the abort";
  458.       setb FXX               ;;;          flag the abort;
  459.    2: ajmp CommandLoop       ;;;       break;
  460. 1: ajmp CommandLoop          ;;;    }
  461.  
  462. seg data
  463. INTERP_SEG: ds 0x10
  464. TEST_SEG:   ds 0x10
  465.  
  466. seg code
  467. global main:              ;;; main() {
  468.    acall SetTest          ;;;    SetTest();
  469.    mov R0, #INTERP_SEG
  470.    mov DPTR, #Interpreter
  471.    acall Spawn            ;;;    Spawn(0xc8, Interpreter);
  472.    setb EA                ;;;    "turn on multi-tasker";
  473. ret                       ;;; }
  474.